package com.elong.businesstravel.modules.home.ticket.ticketshow;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.utils.DateUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.basefragment.BaseFragment;
import com.elong.businesstravel.entitys.TrainTicket;
import com.elong.businesstravel.modules.home.ticket.adapter.TicketListAdapter;
import com.elong.businesstravel.protocol.ticket.StartEndInfosRequest;
import com.elong.businesstravel.protocol.ticket.StartEndInfosResponse;
import com.umeng.socialize.net.ac;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

/**
 * 火车票列表
 * 
 * @author lenovo
 * 
 */
public class TicketListFragment extends BaseFragment {

	private PullToRefreshListView mPullToRefreshListView;
	private TicketListAdapter mTicketListAdapter;

	private TicketListActivity mTicketListActivity;

	private List<TrainTicket> mTrainTickets = new ArrayList<TrainTicket>();

	private int mNum = 0;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		mNum = getArguments() != null ? getArguments().getInt("num") : 0;
		DebugLog.logI("TicketListFragment : " + mNum);

		mTicketListActivity = (TicketListActivity) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_ticket_list, null);
		setViewData(view);
		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		DebugLog.logI(this.getClass().toString());
		getTrainTicketList();
	}

	@Override
	protected void setViewData(View view) {

		mPullToRefreshListView = (PullToRefreshListView) view
				.findViewById(R.id.listView);
		mPullToRefreshListView.setMode(Mode.PULL_FROM_START);
		mTicketListAdapter = new TicketListAdapter(mParentContext,
				mTrainTickets);
		mPullToRefreshListView.setAdapter(mTicketListAdapter);

		setListener(view);
	}

	@Override
	protected void setListener(View view) {
		
		mPullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {

			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				 getTrainTicketList();
			}
		});
	}

	private void getTrainTicketList() {
		String startDateString = mTicketListActivity.getTicketSearchCondition()
				.getStartDateByIndex(mNum);

		NetworkClient
				.getNetworkClient()
				.PostRequest(
						new StartEndInfosRequest(
								mParentContext,
								mTicketListActivity.getTicketSearchCondition().startStation,
								mTicketListActivity.getTicketSearchCondition().endStation,
								startDateString,
								mTicketListActivity.getTicketSearchCondition().trainType,
								mTicketListActivity.getTicketSearchCondition().hasYp),
						new StartEndInfosResponse() {
							@Override
							public void onResponse(
									ResponseHandlerInterface responseHandlerInterface) {
								HandlerPostUI.getHandlerPostUI(new Runnable() {

									@Override
									public void run() {
										mTrainTickets.clear();
										mTrainTickets.addAll(trainTickets);
										mTicketListAdapter
												.notifyDataSetChanged();
										
										mPullToRefreshListView.onRefreshComplete();
									}
								});
							}
						});
	}

}
